import {
  cancelUserFollowAPI,
  getPersonData,
  getUser,
  login,
  sendCode,
  updatePartialData,
  updatePhoto,
  userFollowAPI,
} from '@/api/user';
import store, { AppDispatch } from '@/store/index';

// import http from '@/utils/request';
// import { AppDispatch } from '..';
import { setToken } from '@/utils/storage';
import { Person } from '../reducer/user';

//发送验证码action
export function userActionCreator(mobile: string) {
  return async (dispatch: AppDispatch) => {
    await sendCode(mobile);
  };
}

// 登录action
export function LoginActionCreator({
  mobile,
  code,
}: {
  mobile: string;
  code: string;
}) {
  return async (dispatch: AppDispatch) => {
    const res = await login({ mobile, code });
    // console.log(35, res);
    setToken(res.data);
    dispatch({
      type: 'user/getToken',
      payload: res.data,
    });
  };
}

// 获取用户自己信息action
export function getUserActionCreator() {
  return async (dispatch: AppDispatch) => {
    const res = await getUser();
    // console.log(41, res);
    dispatch({
      type: 'user/userInfo',
      payload: res.data,
    });
  };
}

// 获取用户个人资料action
export function getPersonActionCreator() {
  return async (dispatch: AppDispatch) => {
    const res = await getPersonData();
    // console.log(51, res);
    dispatch({
      type: 'user/person',
      payload: res.data,
    });
  };
}

// 修改个人资料头像
export function uploadPhotoActionCreator(data: FormData) {
  return async (dispatch: AppDispatch) => {
    const res = await updatePhoto(data);
    // console.log(63, res.data.photo);
    dispatch({
      type: 'user/uploadPhoto',
      payload: res.data,
    });
  };
}

// 修改个人资料性别
export function uploadGenderActionCreator(data: Partial<Person>) {
  return async (dispatch: AppDispatch) => {
    await updatePartialData(data);
    // console.log(82, res);
    // console.log(83, data);

    dispatch({
      type: 'user/uploadGender',
      payload: data,
    });
  };
}

// 修改用户昵称
export function uploadUserNameActionCreator(data: Partial<Person>) {
  return async (dispatch: AppDispatch) => {
    await updatePartialData(data);
    // console.log(94, res);
    dispatch({
      type: 'user/uploadUserName',
      payload: data,
    });
  };
}

// 修改用户简介
export function uploadIntroActionCreator(data: Partial<Person>) {
  return async (dispatch: AppDispatch) => {
    await updatePartialData(data);
    // console.log(94, data);
    dispatch({
      type: 'user/uploadIntro',
      payload: data,
    });
  };
}

// 修改用户简介
export function uploadBirthDayActionCreator(data: Partial<Person>) {
  return async (dispatch: AppDispatch) => {
    await updatePartialData(data);
    // console.log(94, data);
    dispatch({
      type: 'user/uploadBirthDay',
      payload: data,
    });
  };
}

// 关注用户和取消用户
export function changeUserFollowActionCreator(id: string) {
  return async (dispatch: AppDispatch) => {
    const { detail } = store.getState().articleList;
    if (detail.is_followed === false) {
      await userFollowAPI(id);
      dispatch({ type: 'article/changeArticleDetailFollow' });
    } else {
      await cancelUserFollowAPI(id);
      dispatch({ type: 'article/changeArticleDetailFollow' });
    }
  };
}
